package com.zhonghan.recharge.service.impl;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.dao.ZhCardReceiveDao;
import com.zhonghan.recharge.dao.ZhFillCardDao;
import com.zhonghan.recharge.pojo.ZhCardReceive;
import com.zhonghan.recharge.service.ZhCardReceiveService;
@Service
public class ZhCardReceiveServiceImpl implements ZhCardReceiveService{
    @Autowired
    private ZhCardReceiveDao zhCardReceiveDao;
    
    @Autowired
    private ZhFillCardDao zhFillCardDao;
    @Override
    public long getZhCardReceiveRowCount(){
        return zhCardReceiveDao.getZhCardReceiveRowCount();
    }
    @Override
    public List<ZhCardReceive> selectZhCardReceive(){
        return zhCardReceiveDao.selectZhCardReceive();
    }
    @Override
    public ZhCardReceive selectZhCardReceiveByObj(ZhCardReceive obj){
        return zhCardReceiveDao.selectZhCardReceiveByObj(obj);
    }
    @Override
    public ZhCardReceive selectZhCardReceiveById(Integer id){
        return zhCardReceiveDao.selectZhCardReceiveById(id);
    }
    @Override
    public int insertZhCardReceive(ZhCardReceive value){
        return zhCardReceiveDao.insertZhCardReceive(value);
    }
    @Override
    public int insertNonEmptyZhCardReceive(ZhCardReceive value){
        return zhCardReceiveDao.insertNonEmptyZhCardReceive(value);
    }
    @Override
    public int insertZhCardReceiveByBatch(List<ZhCardReceive> value){
        return zhCardReceiveDao.insertZhCardReceiveByBatch(value);
    }
    @Override
    public int deleteZhCardReceiveById(Integer id){
        return zhCardReceiveDao.deleteZhCardReceiveById(id);
    }
    @Override
    public int updateZhCardReceiveById(ZhCardReceive enti){
        return zhCardReceiveDao.updateZhCardReceiveById(enti);
    }
    @Override
    public int updateNonEmptyZhCardReceiveById(ZhCardReceive enti){
        return zhCardReceiveDao.updateNonEmptyZhCardReceiveById(enti);
    }

    public ZhCardReceiveDao getZhCardReceiveDao() {
        return this.zhCardReceiveDao;
    }

    public void setZhCardReceiveDao(ZhCardReceiveDao zhCardReceiveDao) {
        this.zhCardReceiveDao = zhCardReceiveDao;
    }
    public ZhCardReceive selectZhCardReceiveBySequence(String sequence){
    	return this.zhCardReceiveDao.selectZhCardReceiveBySequence(sequence);
    }
    /**
     * 通过名字去查询领卡人
     * @param pageNum
     * @param pageSize
     * @param reciveName
     * @return
     */
    public Result selectZhCardReceiveByName(String reciveName,String beginTime,String endTime,Integer pageNum,Integer pageSize){
    	pageNum = pageNum == null ? 0 : pageNum;
    	pageSize = pageSize == null ? 0 : pageSize;
    	
    	ZhCardReceive receive = new ZhCardReceive();
    	
    	if(StringUtils.isNotEmpty(reciveName)){
    		receive.setReceiveName(reciveName);
    	}
    	if(StringUtils.isNotEmpty(beginTime)){
    		receive.setBeginTime(beginTime);
    	}
    	if(StringUtils.isNotEmpty(endTime)){
    		receive.setEndTime(endTime);
    	}
    	
    	receive.setOrderBy("sequence_time");
    	receive.setSortBy("desc");
    	Result result = new Result();
		try {
			
			PageHelper.startPage(pageNum, pageSize);
			List<ZhCardReceive> selectZhCardReceiveByName = zhCardReceiveDao.selectZhCardReceiveByName(receive);
			PageInfo<ZhCardReceive> pageInfo = new PageInfo<>(selectZhCardReceiveByName);
			
			//查询使用的卡数
			for (ZhCardReceive zhCardReceive : selectZhCardReceiveByName) {
				long uses = zhFillCardDao.getZhFillCardRowCountBySequences(zhCardReceive.getSequenceBegin(), zhCardReceive.getSequenceEnd(), 2);
				zhCardReceive.setUseCards( (int)uses);
			}
			
			
			result.setCode(ResultStatus.SUCCESS.getCode());
			result.setData(pageInfo);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
		}
    	return result;
    }
    /**
     * 修改领取的状态
     * @param sequenceBegin 开始卡号
     * @param sequenceEnd 结束卡号
     * @return
     */
    public int updateZhCardReceiveByStatus(String sequenceBegin,String sequenceEnd){
    	return zhCardReceiveDao.updateZhCardReceiveByStatus(sequenceBegin, sequenceEnd);
    }
    /**
     * 通过卡段查询 使用情况，未使用情况
     * @param sequence1
     * @param sequence2
     */
    public ZhCardReceive selectUsesBySequences(ZhCardReceive cardReceive) {
    	
    	//已消费数据量
    	long uses = zhFillCardDao.getZhFillCardRowCountBySequences(cardReceive.getSequenceBegin(), cardReceive.getSequenceEnd(), 2);
    	//没有激活数量
    	long  not_activation= zhFillCardDao.getZhFillCardRowCountBySequences(cardReceive.getSequenceBegin(), cardReceive.getSequenceEnd(), -1);
    	
    	//未消费数量
    	long not_uses1 = zhFillCardDao.getZhFillCardRowCountBySequences(cardReceive.getSequenceBegin(), cardReceive.getSequenceEnd(), 0);
    	long not_uses2 = zhFillCardDao.getZhFillCardRowCountBySequences(cardReceive.getSequenceBegin(), cardReceive.getSequenceEnd(), 1);
    	
    	
    	cardReceive.setUseCards((int)uses);
    	cardReceive.setNotUses1((int)not_uses1);
    	cardReceive.setNotUses2((int)not_uses2);
    	
    	cardReceive.setNotActivation((int)not_activation);
    	
    	
    	return cardReceive;
    }
    /**
     * 修改端口
     */
    public int updateZhCardReceiveByPort(Long id,String portToken,String portAppkey,
			String portAppsecret,String portUrl,String portPhone){
    	return zhCardReceiveDao.updateZhCardReceiveByPort(id, portToken, portAppkey, portAppsecret, portUrl, portPhone);
    }
	@Override
	public Result addReceiver(String sequenceBegin, String sequenceEnd, String receiveName, Integer receiveCount,
			Integer facePrice) {
		
		Result result = new Result();
		
		List<ZhCardReceive> receiveList = zhCardReceiveDao.selectZhCardReceiveExist(sequenceBegin,sequenceEnd);
		
		if(receiveList != null && receiveList.size() > 0){
			result.setFailure("-1", "当前添加卡段已存在,请检查后再重试!");
			return result;
		}else{
			ZhCardReceive receive = new ZhCardReceive();
			receive.setSequenceBegin(sequenceBegin);
			receive.setSequenceEnd(sequenceEnd);
			receive.setFacePrice(facePrice);
			receive.setReceiveName(receiveName);
			receive.setSequenceTime(new Date());
			receive.setReceiveCount(receiveCount);
			receive.setAuthCode(0);
			try {
				int num = zhCardReceiveDao.insertZhCardReceive(receive);
				if(num > 0){
					result.setSuccess("1", null, "添加成功!");
				}else{
					result.setFailure("-1", "添加失败!");
				}
				
			} catch (Exception e) {
				result.setFailure("-1", "系统异常,请稍后再试!");
				e.printStackTrace();
			}
		}
		
		return result;
	}
}